import pagination from '../../../components/common/pageComponent'
import instance from '@/api/index.js'

export default {
  name: "Draft",
  data(){
    return {
      tables: [
      ],
      show: false,//loading
      item: '',
      productList: [],//获取商品列表
      productListParam: {},//商品列表查询
      analysisStatus: new Map,//状态解析
      productClassify: [],//根据分类层级查询分类信息
      forEachIndex: 0,//递归下标
      productClassifyMap: new Map,//获取子叶商品分类
      productSelect: [],//解析Map数据
      selectCategoryId: '',
      selectGoodsEn:'',
      brandSelectChoose: "",//品牌下拉选择
      brandSelectList: [],//品牌下拉列表
      productName: "",//搜索商品名称和商品货号
      activeIssue: 0,//添加class
      errorShow: false,//错误显示
      errorMsg: '',//错误信息
      errorTitle: '',//错误提示标题
      errorClass: false,//错误样式
      pages: {
        total: 0,
        display: 1,
        current: 1
      },

      allCategoryAnalysis: [],//商品分类
      allCategory: [],
      classificationList: [],

      publishList: [],//获取ebay商品列表

      batchAdd: [],//批量刊登发布
      checkModal: [],
      allCheckBox: false,
      savePublishList:[],
      active:0,
      dialogFormVisible:false,
      btnSeek:[
        {
          status:'',
          name:'全部'
        },
        {
          status:'1',
          name:'已刊登'
        },
        {
          status:'0',
          name:'待刊登'
        }
      ],
      formData: {
        // 传给后台的id
        inspectObject: "",
        // 输入框显示的
        inspectObjectName: ""
      },
      // 是否显示巡检对象的树状选择器
      isShowSelect: false,
      // 巡检对象的选项数据
      inspectionObjectOptions: [],
      // 巡检对象的选项的配置参数
      defaultProps: {
        id: "categoryId",
        label: 'categoryName',
        children: 'children'
      },
    }

  },
  mounted(){
  },
  methods:{
    handleNodeClick(data, Node) {
      // 如果是顶级父节点
      this.formData.inspectObjectName = data.categoryName;
      this.formData.inspectObject = data.categoryId;
      this.selectCategoryId = data.categoryId;
      // 关闭选择器
      this.isShowSelect = false;
    },
    //分页
    pagechange:function(currentPage){
      this.productListParam.page = currentPage;
      this.pages.current = currentPage;
      this.getProductListFn();
    },

    //获取ebay商品列表
    getProductListFn:function(){
      let vm = this;
      vm.show = true;
      this.allCheckBox = false;
      this.checkModal = [];
      vm.productListParam.page = this.pages.current;
      vm.productListParam.rows = 20;
      vm.pages.display = vm.productListParam.rows;
      instance({
        method:'post',
        url: 'v1/ebay/goods/getList',
        data:vm.productListParam
      }).then(function (data) {
        console.log(data)
        if(data.data.success){
          console.log(data)
          vm.productList = data.data.data.list;
          vm.pages.total = data.data.data.total;
          vm.show = false;
        }
      }).catch(function (err) {
        console.log(err)
      })
    },

    //商品状态查询
    seekPublishStatus:function(_index,_status){
      this.active = _index;
      this.productListParam.status = _status;
      if (!this.productListParam.name){
        delete this.productListParam.name
      }
      delete this.productListParam.page;
      delete this.productListParam.rows;
      if (!this.productListParam.ebaySku){
        delete this.productListParam.ebaySku
      }
      this.getProductListFn();
    },

    //定时信息提示
    setTime: function () {
      let vm = this;
      setTimeout(function () {
        vm.errorShow = false;
      }, 2500)
    },

    //关闭信息提示
    closeError: function () {
      this.errorShow = false;
    },

    //获取所有商品分类树
    getAllCategory: function () {
      let vm = this;
      instance({
        method: 'post',
        url: '/v1/goods/category/getAll',
      }).then(function (data) {
        if (data.data.success) {
          let menus = data.data.data;
          vm.allMenu = menus;
          console.log(vm.allMenu)
          vm.menuListCheck = [];
          vm.getData(null, menus);
          vm.depositMenus = [];
          for (let i = 0; i < vm.menuListCheck.length; i++) {
            if (vm.menuListCheck[i].categoryParentId == null) {
              vm.depositMenus.push(vm.menuListCheck[i])
            }
          }
          vm.allCategoryAnalysis = vm.depositMenus;
        }
        console.log('vm.allCategoryAnalysis', vm.allCategoryAnalysis)
        //
        // vm.classificationListL2 = vm.allCategoryAnalysis
      })
    },

    getData: function (id, arry) {
      let vm = this;
      var childArry = this.familyTree(id, arry);
      if (childArry.length > 0) {
        for (var i = 0; i < childArry.length; i++) {
          let obj = {};
          obj = childArry[i];
          childArry[i].children = this.familyTree(childArry[i].categoryId, arry);
          childArry[i].show = false;
          vm.menuListCheck.push(obj);
          vm.getData(childArry[i].categoryId, arry);
        }
      }
    },
    //
    familyTree: function (id, arry) {
      var newArry = new Array();
      for (var i in arry) {
        if (arry[i].categoryParentId == id)
          newArry.push(arry[i]);
      }
      return newArry;
    },

    //重置
    resetProduct:function(){
      for (let key in this.productListParam){
        this.productListParam[key] = null;
      }
      this.formData.inspectObjectName = '';
      this.productName = '';
      this.selectGoodsEn = '';
      this.productListParam.status = '';
      this.active = 0;
      this.allCheckBox = false;
      this.checkModal = [];
      this.getProductListFn();
    },

    //搜索
    searchProduc:function(){
      // this.deleteClassifyCondition();
      this.productListParam.name = this.productName;
      this.productListParam.ebaySku = this.selectGoodsEn;
      for (let key in this.productListParam){
        if(!this.productListParam[key]){
          delete this.productListParam[key];
        }
      }
      this.getProductListFn();
    },

    //循环删除查询条件
    deleteClassifyCondition(){
      for (let key in this.productListParam) {
        this.productListParam[key] = null;
      }
    },

    //商品品牌下拉列表
    getProductBrand:function(){
      let vm = this;
      instance({
        method:'post',
        url: '/v1/goods/brand/getList',
        data:vm.productListParam
      }).then((data)=>{
        vm.brandSelectList = data.data.data;
      })
    },

    //查看草稿
    examineEbay:function (_id, _type) {
      let vm = this;
      if(_type == 0){
        this.$router.push({
          path:'/v1/ebay/draft/edit/getData',
          query:{
            draftId:_id,
            readOnly:true
          }})
      }else {
        this.$router.push({
          path:'/v1/ebay/draft/edit/getData',
          query:{
            draftId:_id,
            readOnly:false,
          }})
      }

      // instance({
      //   method:'post',
      //   url:'/v1/ebay/draft/edit/getData',
      //   params:{
      //     draftId:id
      //   }
      // }).then(function (data) {
      //   console.log(data);
      // })
    },

    //批量刊登发布
    getbatchAdd:function (_draftIdList) {
      let vm = this;
      let idList = [];
      if(_draftIdList instanceof Array){
        idList = _draftIdList
      }else {
        idList.push(_draftIdList)
      }
      instance({
        method:'post',
        url:'/v1/ebay/draft/batchAddItem/getList',
        data:idList
      }).then(function (data) {
        if (data.data.success){
          vm.batchAdd = data.data.data;
          console.log(data)
        }
      })
    },

    getbatchAddFn:function(_draftId){
      this.checkModal = [];
      this.savePublishList = [];
      this.allCheckBox = false;
      this.savePublishList.push(_draftId);
      this.getbatchAdd(_draftId)
    },

    allPublish:function () {
      if(this.checkModal.length == 0){
        this.errorClass = false;
        this.errorShow = true;
        this.errorTitle = "操作失败";
        this.errorMsg = "请至少选择一个产品发布";
        this.setTime();
      }else {
        this.batchAdd = [];
        this.savePublishList = [];
        this.savePublishList = this.checkModal;
        console.log(this.savePublishList)
        this.getbatchAdd(this.checkModal)
      }
    },

    allCheckBoxFn:function () {
      let vm = this;
      if(this.allCheckBox){
        this.productList.forEach(function (item) {
          if(item.status == 0){
            vm.checkModal.push(item.draftId)
          }
        })
      }else {
        vm.checkModal = [];
      }
    },

    //刊登保存
    savePublish:function () {
      let vm = this;
      vm.dialogFormVisible = true;
      console.log(this.savePublishList)
      instance({
        method:'post',
        url:'/v1/ebay/draft/batchAddItem/confirm',
        data:this.savePublishList
      }).then(function (data) {
        console.log(data);
        if(data.data.success){
          vm.getProductListFn();
          vm.dialogFormVisible = false;
        }else {
          vm.errorClass = false;
          vm.errorShow = true;
          vm.errorTitle = "确认发布失败";
          vm.errorMsg = data.data.msg;
          vm.setTime();
          vm.dialogFormVisible = false
        }
      })
    }
  },
  created(){

    //获取商品列表数据
    this.getProductListFn();
    //获取商品分类下拉列表
    this.getAllCategory();
    //商品品牌下拉列表
    this.getProductBrand();
    this.analysisStatus.set(0,"未上架");
    this.analysisStatus.set(1,"上架中");
    this.analysisStatus.set(2,"删除");
  },
  components: {
    pagination
  },
  filters:{
    reserveDecimal:function (val) {
      var f = parseFloat(val);
      if (isNaN(f)) {
        return 0;
      }
      var f = Math.round(val*100)/100;
      var s = f.toString();
      var rs = s.indexOf('.');
      if (rs < 0) {
        rs = s.length;
        s += '.';
      }
      while (s.length <= rs + 2) {
        s += '0';
      }
      return s;
    }
  }
}
